package org.cc.cloud.controller;

import org.cc.cloud.MyLoadBalanced;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.HystrixCircuitBreaker;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.ZoneAwareLoadBalancer;

@Controller
@Configuration
public class ClientController {

	@Bean
	@LoadBalanced
	public RestTemplate getRestTemplate()
	{
		return new RestTemplate();
	}
	@Bean
	@MyLoadBalanced
	public RestTemplate getMyRestTemplate()
	{
		return new RestTemplate();
	}
	@GetMapping("/ribbonCall")
	@ResponseBody
	public String rouler(){
		RestTemplate rest=getRestTemplate();
		return rest.getForObject("http://first-provider/call/1",String.class);
	}
	@GetMapping("/ribbonCall1")
	@ResponseBody
	public String rouler1(){
		RestTemplate rest=getMyRestTemplate();
		return rest.getForObject("http://first-provider/call/1",String.class);
	}
	@Autowired
	private LoadBalancerClient client;
	@GetMapping("/lb")
	@ResponseBody
	public ServiceInstance lb(){
		ServiceInstance s= client.choose("first-provider");
		return s;
	}
	@Autowired
	private SpringClientFactory factory;
	@GetMapping("/fa")
	@ResponseBody
	public String factory(){
		ZoneAwareLoadBalancer lb= (ZoneAwareLoadBalancer)factory.getLoadBalancer("default");
		System.out.println(lb.getRule().getClass().getName());
		return "";
	}
}
